Ext.data.RestProxy= function(conn){
    Ext.data.RestProxy.superclass.constructor.call(this, conn);
    this.conn = conn;
    this.conn.url = null;

    this.useAjax = !conn || !conn.events;

    var actions = Ext.data.Api.actions;
    this.activeRequest = {};
    for (var verb in actions) {
        this.activeRequest[actions[verb]] = undefined;
    }
};
Ext.extend(Ext.data.RestProxy,Ext.data.HttpProxy,{
alias:'proxy.restjson',

buildJson:function(params){
	var o={};
	for(var i in params){
		var k=i.split('.');
		var v=params[i];
		if(k.length>1){
			var n=o;
			for(var j=0;j<k.length-1;j++){
				var p=k[j];
				n[p]=n[p]||{};
				n=n[p];
			}
			n[k[k.length-1]]=v;
		}else{
			o[i]=v;
		}
	}
	var data=Ext.encode(o);
	return data;
},


request :  function(action, rs, params, reader, cb, scope, arg)  {
	var  o = {
            method: "POST",
            request: {
                callback : cb,
                scope : scope,
                arg : arg
            },
            reader: reader,
            callback : this.createCallback(action, rs),
            scope: this
        };

		o["jsonData"]=this.buildJson(params);
        this.conn.url = this.buildUrl(action, rs);

        if(this.useAjax){

            Ext.applyIf(o, this.conn);
           var headers= o["headers"]||{};
           headers["content-type"]="application/json;charset=utf-8";
            // If a currently running read request is found, abort it
            if (action == Ext.data.Api.actions.read && this.activeRequest[action]) {
                Ext.Ajax.abort(this.activeRequest[action]);
            }
            this.activeRequest[action] = Ext.Ajax.request(o);
        }else{
            this.conn.request(o);
        }
        // request is sent, nullify the connection url in preparation for the next request
        this.conn.url = null;
},
});
